
package com.hlkj.pay.controller.api;
/*
 * Hlpay-Plus aggregate payment system.
 * Copyright (c) 2024-2025 Hlpay Team Copyright has the right of final interpretation.
 */

import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.hlkj.framework.common.pojo.CommonResult;
import com.hlkj.pay.app.devices.IMerchantQrAppService;
import com.hlkj.pay.common.CommonResultCode;
import com.hlkj.pay.controller.BaseController;
import com.hlkj.pay.enums.CommonEnum;
import com.hlkj.pay.infrastructure.model.devices.MerchantQrDO;
import com.hlkj.pay.infrastructure.model.merchant.MerchantInfoDO;
import com.hlkj.pay.service.devices.IMerchantQrService;
import com.hlkj.pay.service.merchant.IMerchantService;
import com.hlkj.pay.vo.devices.resp.MerchantQrDetailResp;
import com.hlkj.pay.vo.openapi.order.resp.PayOrderSubmitApiResp;
import com.hlkj.pay.vo.order.req.QrOrderParamReq;

import cn.hutool.core.bean.BeanUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;

/**
 * @author HlpayTeam
 * @date 2025/01/20 11:03
 */
@Tag(name = "商户码牌")
@RestController
@RequestMapping("/api/merchant/qr")
@RequiredArgsConstructor
public class QrController extends BaseController {

    private final IMerchantQrService merchantQrService;

    private final IMerchantQrAppService merchantQrAppService;
    private final IMerchantService merchantService;

    @Operation(summary = "商户码牌详情")
    @GetMapping("/detail")
    CommonResult<MerchantQrDetailResp> detailResp(@RequestParam(value = "code") String code) {
        MerchantQrDO merchantQrDO = merchantQrService.queryDetail(code);
        if (merchantQrDO == null) {
            return CommonResult.error(CommonResultCode.DATA_NOT_EXIST);
        }
        if(!StringUtils.hasText(merchantQrDO.getSn())){
            return CommonResult.error(CommonResultCode.DATA_NOT_EFFECT.getCode(),"二维码未绑定商户");
        }
        if (CommonEnum.YES_NO_TYPE.NO.getCode().equals(merchantQrDO.getStatus())) {
            return CommonResult.error(CommonResultCode.DATA_NOT_EFFECT.getCode(),"二维码未启用");
        }
        MerchantInfoDO merchantInfoDO = merchantService.queryMerchant(merchantQrDO.getSn());
        MerchantQrDetailResp merchantQrDetailResp = BeanUtil.copyProperties(merchantQrDO, MerchantQrDetailResp.class);
        if(merchantInfoDO!=null){
            merchantQrDetailResp.setMerchantName(merchantInfoDO.getName());
            merchantQrDetailResp.setShortName(merchantInfoDO.getShortName());
        }
        // if("".equals(merchantQrDO.getSubType())){
        //
        // }
        //
        //
        // MerchantAppQueryDto merchantAppQueryDto=new MerchantAppQueryDto();
        // merchantAppQueryDto.setAppId(merchantQrDO.getAppId());
        // List<MerchantAppChannelDO> merchantAppChannelList = merchantApplicationService.queryChannelInfoListEffectProvideAppId(merchantAppQueryDto);
        // MerchantAppChannelDO merchantAppChannelDO = MerchantAppChannelRouterUtil.router(merchantAppChannelList);
        return CommonResult.success(merchantQrDetailResp);
    }

    @Operation(summary = "码牌订单提交")
    @PostMapping("/submit")
    CommonResult<PayOrderSubmitApiResp> submitOrder(@Validated @RequestBody QrOrderParamReq qrOrderParamReq) {
        return merchantQrAppService.submitOrder(qrOrderParamReq);
    }

}
